#include <iostream>
#include <cstring>
#include <string>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

int main()
{
    int fd=socket(AF_INET,SOCK_STREAM,0);
    if(fd<0)
    {
        printf("客户端：获取套接字失败！\n");
        return -1;
    }
    struct sockaddr_in server_addr;
    memset(&server_addr,0,sizeof(server_addr));
    server_addr.sin_family=AF_INET;
    server_addr.sin_port=htons(6666);
    server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
    int connfd=connect(fd,(const struct sockaddr*)&server_addr,sizeof(server_addr));

    if(connfd<0)
    {
        perror("客户端：连接失败\n");
        return -1;
    }
    int ret;
    std::string buf;
    buf.resize(1024);
    while(1)
    {
        std::getline(std::cin,buf);
        ret=write(fd,buf.c_str(),sizeof(buf));
        if(ret<0)
        {
            perror("客户端：发送数据失败！\n");
            return -1;
        }
        else if(ret==0)
        {
            break;
        }
        char returnBuf[1024];
        memset(returnBuf,0,sizeof(returnBuf));

        ret=read(fd,(void *)returnBuf,sizeof(returnBuf));
        if(ret<0)
        {
            perror("客户端：接收响应数据失败！\n");
        }
        else if(ret==0)
        {
            break;
        }
        std::cout<<returnBuf<<std::endl;
    }
    close(fd);
    return 0;
}